# build in python3.5.2
import math
dataset = [[1, 20, 1], [2, 21, 0], [3, 22, 1], [4, 22, 0]]  # 最后一项是分类 0,1

# 数据集分类，字典真好用
def separateByClass(dataset):
    separated = {}
    for i in range(len(dataset)):
        vector = dataset[i]
        # 字典里没有对应的键值则添加
        if (vector[-1] not in separated):
            separated[vector[-1]] = []
        separated[vector[-1]].append(vector)
    return separated

# 求均值
def mean(numbers):
    return sum(numbers) / float(len(numbers))

# 均方差
def stdev(numbers):
    avg = mean(numbers)
    variance = sum([pow(avg - x, 2) for x in numbers]) / float(len(numbers) - 1)
    return math.sqrt(variance)

# 汇总数据集,会输出列表，列表第一项是均值，第二项是偏差
def summarize(dataset):
    summaries = []
    # *让zip接受任意多的参数
    for attribute in zip(*dataset):
        summaries.append((mean(attribute), stdev(attribute)))
    del summaries[-1]
    return summaries

print('总结',summarize(dataset))

# 把对应标签的数据进行汇总，值域里第一项是均值，第二项是偏差
def summarizeByClass(dataset):
    separated = separateByClass(dataset)
    summaries = {}
    for classValue, instances in separated.items():
        summaries[classValue] = summarize(instances)
    return summaries

print('按类总结',summarizeByClass(dataset))

# 计算高斯概率密度
def calculateProbability(x, mean, stdev):
    exponent = math.exp(-(math.pow(x - mean, 2) / (2 * math.pow(stdev, 2))))
    return (1 / (math.sqrt(2 * math.pi) * stdev)) * exponent

def calculateClassProbabilities(summaries, inputVector):
    probabilities = {}
    for classValue, classSummaries in summaries.iteritems():
        probabilities[classValue] = 1
        for i in range(len(classSummaries)):
            mean, stdev = classSummaries[i]
            x = inputVector[i]
            probabilities[classValue] *= calculateProbability(x, mean, stdev)
    return probabilities

def predict(summaries, inputVector):
    probabilities = calculateClassProbabilities(summaries, inputVector)
    bestLabel, bestProb = None, -1
    for classValue, probability in probabilities.items():
        if bestLabel is None or probability > bestProb:
            bestProb = probability
            bestLabel = classValue
    return bestLabel

def getPredictions(summaries, testSet):
    predictions = []
    for i in range(len(testSet)):
        result = predict(summaries, testSet[i])
        predictions.append(result)
        return predictions

def getAccuracy(testSet, predictions):
    correct = 0
    for x in range(len(testSet)):
        if testSet[x][-1] == predictions[x]:
            correct += 1
    return (correct / float(len(testSet))) * 100.0

testSet = [[1,1,1,'a'], [2,2,2,'a'], [3,3,3,'b']]
predictions = ['a', 'a', 'a']
print(getAccuracy(testSet, predictions))